//
//  GameViewController.m
//  BeatPlane
//
//  Created by LZXuan on 15-5-6.
//  Copyright (c) 2015年 轩哥. All rights reserved.
//

#import "GameViewController.h"
#import "Plane.h"
#import "Bullet.h"
#import "Enemy.h"


#define kScreenSize [UIScreen mainScreen].bounds.size
#define kBulletNumber 30


@interface GameViewController ()
{
    //游戏界面背景
    UIImageView *_backImageView;
    Plane *_plane;
    
    //定时器
    NSTimer *_timer;
    //方向视图
    UIView *_towardsView;
    //存放子弹
    NSMutableArray *_bulletArr;
    
    //敌人数组
    NSMutableArray *_enemyArr;
    
}
@end

/*
 游戏界面 就是来管理游戏的
 管理 飞机 敌人 子弹
 
 */
@implementation GameViewController
- (void)dealloc {
    [_enemyArr release];
    [_bulletArr release];
    [_towardsView release];
    [_backImageView release];
    [super dealloc];
}

//代码入口
- (void)viewDidLoad {
    [super viewDidLoad];
    [self gameInit];
}

/*
 1.创建背景
 2.设计飞机
 3.设计方向按钮 控制飞机移动
 4.飞机发射子弹
 5.设计敌人 敌人移动
 6.碰撞检测
 7.其他
 
 */

#pragma mark - 游戏初始化
- (void)gameInit {
    //先创建背景
    [self creatGameBack];
    
    [self creatEnemys];
    //创建飞机
    [self creatPlane];
    //创建方向
    [self creatTowardsView];
    //创建子弹
    [self creatBullets];
    
    
}
#pragma mark - 界面将要显示的时候
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    //创建定时器
    [self creatTimer];
}
//界面将要离开屏幕的时候调用
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    if ([_timer isValid]) {//判断定时器是否有效
        [_timer invalidate];//终止定时器
        _timer = nil;
    }
}


#pragma mark - 定时器
//定时器 控制器 飞机的移动 敌人的移动 子弹移动 碰撞检测
- (void)creatTimer {
    if (_timer) {
        [_timer invalidate];//销毁
        _timer = nil;
    }
    _timer = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(timerClick) userInfo:nil repeats:YES];
}
//定时器 触发的方法
- (void)timerClick {
    //控制飞机的移动
    [_plane planeAction];
    //子弹飞
    [self bulletFly];
    [self enemysMove];
    
    [self isHitting];
}
#pragma mark - 碰撞检测
- (void)isHitting {
    //检测子弹和敌人是否有碰撞
    for (Bullet *bullet in _bulletArr) {
        //子弹在屏幕外不用检测
        if (bullet.frame.origin.y <= - bullet.frame.size.height) {
            continue;//遍历下一个子弹
        }
        //遍历所有的敌人
        for (Enemy *enemy in _enemyArr) {
            //碰撞检测
            //检测两个frame 有没有交集
            bool ret = CGRectIntersectsRect(bullet.frame, enemy.frame);
            if (ret) {
                //放在屏幕外 就看不到了
                [enemy resetFrame];
                bullet.frame = CGRectMake(-50, -50, bullet.frame.size.width, bullet.frame.size.height);
            }
        }
    }
}



#pragma mark - 创建敌人
- (void)creatEnemys {
    _enemyArr = [[NSMutableArray alloc] init];
    
    for (NSInteger i = 0; i < 10; i++) {
        Enemy *enemy = [[Enemy alloc] init];
        [self.view addSubview:enemy];
        [_enemyArr addObject:enemy];
        [enemy release];
    }
}
//所有敌人移动  定时器控制
- (void)enemysMove{
    for (Enemy *enemy in _enemyArr) {
        [enemy moveDown];
    }
}


#pragma mark - 创建子弹
- (void)creatBullets {
    _bulletArr = [[NSMutableArray alloc] init];
    
    for (NSInteger i = 0; i < kBulletNumber; i++) {
        //把子弹先放在外面
        Bullet *bullet = [[Bullet alloc] initWithFrame:CGRectMake(-50, -50, 17, 17)];
        [self.view addSubview:bullet];
        //放入数组
        [_bulletArr addObject:bullet];
        [bullet release];
    }
}
#pragma mark - 子弹飞
//定时器控制
- (void)bulletFly {
    static int i = 0;
    
    //子弹 总共kBulletNumber要循环利用
    //如果子弹频繁的创建和销毁 性能会有影响
    //要降低子弹的发射频率 否则就是一条线
    
    if(_plane.isFire) {
        //当前函数调用5次 发射一颗子弹
        if (i % 5==0) {
            //开火状态 重置子弹的位置
            Bullet *bullet = _bulletArr[i/5];
            //重置到飞机的位置 发射位置
            [bullet resetFrame];
        }

        i++;
        if (i == kBulletNumber*5) {
            i = 0;//30个子弹用完 应该把再获取原来的第0个 循环利用
        }
    }
    
    //让所有子弹飞(不管子弹在哪里)
    for (Bullet *bullet in _bulletArr) {
        [bullet moveUp];
    }
}



#pragma mark - 创建方向按钮
- (void)creatTowardsView {
    //方向视图
    _towardsView = [[UIView alloc] initWithFrame:CGRectMake(0, kScreenSize.height-50, 150, 50)];
    _towardsView.backgroundColor = [UIColor clearColor];
    //粘贴到画布
    [self.view addSubview:_towardsView];
    
    //创建方向按钮 把方向按钮粘到 _towardsView上
    NSArray *imageNames = @[@"button_left",@"button_right"];
    for (NSInteger i = 0; i < imageNames.count; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(100*i, 0, 50, 50);
        //设置图片
        [button setBackgroundImage:[UIImage imageNamed:imageNames[i]] forState:UIControlStateNormal];
        button.tag = 301+i;//设置tag 值
        //增加事件
        //按下 改变飞机的方向
        [button addTarget:self action:@selector(changeToward:) forControlEvents:UIControlEventTouchDown];
        //抬起的时候 停止飞机移动
        [button addTarget:self action:@selector(stopPlane) forControlEvents:UIControlEventTouchUpInside];
        [button addTarget:self action:@selector(stopPlane) forControlEvents:UIControlEventTouchUpOutside];
        [_towardsView addSubview:button];
    }
    
    //创建发射按钮
    UIButton *fireButton = [UIButton buttonWithType:UIButtonTypeCustom];
    fireButton.frame = CGRectMake(50, 0, 50, 50);
    [fireButton setBackgroundImage:[UIImage imageNamed:@"fire.png"] forState:UIControlStateNormal];
    [fireButton addTarget:self action:@selector(fireButton:) forControlEvents:UIControlEventTouchUpInside];
    [_towardsView addSubview:fireButton];
    
}
- (void)fireButton:(UIButton *)button {
    if (_plane.isFire) {
        //判断是否正在发射子弹
        _plane.isFire = NO;
        [button setBackgroundImage:[UIImage imageNamed:@"fire"] forState:UIControlStateNormal];
        
    }else{
        _plane.isFire = YES;
        [button setBackgroundImage:[UIImage imageNamed:@"stop"] forState:UIControlStateNormal];
    }
}

#pragma mark - 方向按钮控制的方法
- (void)changeToward:(UIButton *)button {
    switch (button.tag) {
        case 301://左移
            //控制飞机方向
            [_plane startMoveLeft];
            break;
        case 302://右移
            //控制飞机方向
            [_plane startMoveRight];
            break;
            
        default:
            break;
    }
}
- (void)stopPlane {
    //停止移动
    [_plane stopMoveing];
}


#pragma mark - 创建飞机
- (void)creatPlane {
    _plane = [Plane defaultPlane];
    //粘贴到画布
    [self.view addSubview:_plane];
}

- (void)creatGameBack {
    _backImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kScreenSize.width, kScreenSize.height)];
    _backImageView.image = [UIImage imageNamed:@"backgroud.png"];
    //粘贴到画布
    [self.view addSubview:_backImageView];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
